<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%@ page import="org.springframework.web.servlet.support.RequestContextUtils" %>
<%@ page import="org.springframework.security.core.context.SecurityContextHolder" %>
<%@ page import="org.springframework.security.core.userdetails.UserDetails" %>
<%@ page import="it.cst.entity.*" %>
<%@ page import="it.cst.entity.service.*" %>  
<%
    ApplicationContext ctx = RequestContextUtils.getWebApplicationContext(request);
    UserService us = (UserService) ctx.getBean("userService");
	Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	UserDetails userDetails = null;
	userDetails = (UserDetails) principal;
	String userName = userDetails.getUsername();
	User user = us.getUser(userName);
%>    
<!DOCTYPE html> 
<html> 
	<head> 
	<title>CST</title> 
	<meta name="viewport" content="width=device-width, initial-scale=1"> 
	<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.css" />
	<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
	<script src="http://code.jquery.com/mobile/1.2.0-beta.1/jquery.mobile-1.2.0-beta.1.min.js"></script>
	<script>
	function cpw(){
		if(document.getElementById("newPwd").value.length == 0){
			$("#pwdAlert1").popup("open");
			return;
		}
		if(document.getElementById("newPwd").value != document.getElementById("confNewPwd").value){
			$("#pwdAlert2").popup("open");
			return;
		}
		document.getElementById("setNewPwd").submit();
	}
	</script>
	</head>
	<body> 
<div data-role="page">
<div data-role="popup" id="pwdAlert1">
	<p>La password non può essere vuota !<p>
</div>
<div data-role="popup" id="pwdAlert2">
	<p>La password inserita non corrisponde alla conferma !<p>
</div>

	<div data-role="header">
		<h1>Cambio password</h1>
	</div><!-- /header -->
	<div data-role="content">
		<form name="setNewPwd" id="setNewPwd" method="POST" action="setNewPwd" data-ajax="false">
			<div data-role="fieldcontain">
			   <table style="width:100%;align:center;border-collapse:collapse">
				   	<tr>
				   		<td style="width:40%;text-align:left"><label for="basic"><b>UserName :</b></label></td>
				   		<td style="width:60%;text-align:left"><input type="text" name="username" id="username" value="<%=user.getUsername()%>" READONLY/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:40%;text-align:left"><label for="basic"><b>Password :</b></label></td>	
				   		<td style="width:60%;text-align:left"><input type="password" name="oldPwd" id="oldPwd" value="<%=user.getPassword()%>"/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:40%;text-align:left"><label for="basic"><b>Nuova Password :</b></label></td>	
				   		<td style="width:60%;text-align:left"><input type="password" name="newPwd" id="newPwd" value=""/></td>
				   	</tr>
				   	<tr>
				   		<td style="width:40%;text-align:left"><label for="basic"><b>Conferma Nuova Password :</b></label></td>	
				   		<td style="width:60%;text-align:left"><input type="password" name="confNewPwd" id="confNewPwd" value=""/></td>
				   	</tr>
			   </table>
			   <br>
			   <br>
			   <table style="width:100%;align:center;border-collapse:collapse">
			   	<tr>
			   		<td>
			   			<center><input type="button" value="Salva" onclick="cpw()" data-ajax="false"/></center>
			   		</td>
			   	</tr>
			   	<tr>
			   		<td>
			   			<center><a href="userProfile" data-role="button" data-ajax="false">Profilo Utente</a></center>
			   		</td>
			   	</tr>   	
			   	<tr>
			   		<td>
			   			<center><a href="home" data-role="button" data-ajax="false">Home</a></center>
			   		</td>
			   	</tr>
			   </table>
			</div>
		</form>	
	</div><!-- /content -->
</div><!-- /page -->
</body>
</html>